package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo8 {
    public static void main(String[] args) {
        System.out.println("欢迎登录");
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名：");
            String userName = scan.nextLine();
            System.out.println("请输入用户密码：");
            String passWord = scan.nextLine();
            try (
                    Connection connection = DBUtil.getConnection();
            ) {
                String sql = "SELECT id,username,password,nickname,age " +
                        "FROM user " +
                        "WHERE username=? AND password=?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, userName);
                ps.setString(2, passWord);
                ResultSet rs = ps.executeQuery();

                if (rs.next()) {
                    System.out.println("登陆成功");
                    break;
                } else {
                    System.out.println("账号或密码错误");
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
